<template>
  <div id="root" z-index="1" v-show="PasswordState">
    <div class="top">
      <svg
        class="icon icon-erweimajiaobiao"
        aria-hidden="true"
        name="PasswordFn"
        @click="PasswordFn()"
      >
        <use xlink:href="#icon-erweimajiaobiao"></use>
      </svg>
    </div>
    <h1>千锋管理系统</h1>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="captcha">
        <el-input
          v-model="ruleForm.captcha"
          maxlength="5"
          class="ipt"
        ></el-input>
        <span id="svg" @click="RegisterFn()"></span>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">
          提交
        </el-button>
      </el-form-item>
      <div id="note">
        <svg class="icon icon-iphone" aria-hidden="true">
          <use xlink:href="#icon-iphone"></use>
        </svg>
        <p class="note" @click="PasswordNoteState()">切换短信登录</p>
      </div>
    </el-form>
  </div>
</template>

<script>
// 引入api
import {
  getInitializeRegister,
  getRefreshRegister,
  getVerificationRegister,
  getUsernameRegister,
} from "../../../api/login";
// 引入存取组件
import storage from "../../../utils/storage";
export default {
  props: {
    PasswordState: {
      type: Boolean,
      required: true,
    },
  },
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
        captcha: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "用户名可以是字母数字下划线,3-12位",
            trigger: "blur",
          },
          {
            min: 3,
            max: 12,
            message: "用户名可以是字母数字下划线,3-12位",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "密码长度为 3 到 16 位", trigger: "blur" },
          {
            min: 3,
            max: 16,
            message: "密码长度为 3 到 16 位",
            trigger: "blur",
          },
        ],
        captcha: [
          { required: true, message: "验证码必须是5位", trigger: "blur" },
          { min: 5, max: 5, message: "验证码必须是5位", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.initDataFn();
  },
  methods: {
    PasswordFn() {
      this.$emit("PasswordEvent");
    },
    PasswordNoteState() {
      this.$emit("PasswordNoteEvent");
    },
    async RegisterFn() {
      let res = await getRefreshRegister();
      document.getElementById("svg").innerHTML = res.data.img;
    },
    async initDataFn() {
      let res = await getInitializeRegister();
      document.getElementById("svg").innerHTML = res.data.img;
    },
    async captchaFn(captcha, username, password) {
      let res = await getVerificationRegister(captcha);
      if (res.data.status === 200) {
        let arr = await getUsernameRegister(username, password);
        if (arr.data.state) {
          // location.reload();
          this.setFn(arr.data);
          this.$router.push("/");
        } else {
          console.log(res.data.msg);
        }
      } else {
        console.log(res.data.msg);
      }
    },
    setFn({ userInfo, token, permission }) {
      storage.set("userInfo", userInfo);
      storage.set("token", token);
      storage.set("permission", permission);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let captcha = this.ruleForm.captcha;
          let { username, password } = this.ruleForm;
          this.captchaFn(captcha, username, password);
        } else {
          console.log(2, valid);
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.icon-erweimajiaobiao {
  color: #b2c1e8;
  font-size: 60px;
  position: relative;
  left: 340px;
}
.icon-iphone {
  font-size: 28px;
  position: relative;
  left: 140px;
  top: 10px;
}
#svg {
  background: #fff;
  display: inline-block;
  height: 45px;
  width: 150px;
  text-align: center;
  cursor: pointer;
  border: 0;
  border-radius: 0 5px 5px 0;
}
#root {
  width: 400px;
  height: 600px;
  color: #fff;
  border-radius: 10px;
  background: rgba(0, 47, 161, 0.4);
}
/deep/ .el-form-item__label {
  color: #fff;
}
.top {
  width: 100%;
  height: 100px;
}
h1 {
  text-align: center;
  margin: 20px 0;
}
.el-button {
  width: 250px !important;
}
.el-input {
  width: 250px !important;
}
/deep/.el-input__inner {
  height: 45px !important;
  border: 0;
}
.ipt.el-input {
  position: relative;
  top: -17px;
  width: 100px !important;
  border-radius: 5px 0 0 5px !important;
}
/deep/ .ipt .el-input__inner {
  border-radius: 5px 0 0 5px !important;
}
.note {
  font-size: 18px;
  text-decoration: underline;
  position: relative;
  left: 170px;
  top: -20px;
}
// #note {
//   margin-top: -10px;
// }
.el-form-item__label {
  color: #fff !important;
}
</style>
